auto.waitFor();
volKeys();
liyu();

function liyu(){
    while (true){
        sleep(10);
        click(500, 1700);
    }
}

function redpack_ground(){
    while(true){
        // click(157,1895,911,2170)
        sleep(1000);
        click(random(400, 600), random(1900, 2100));
        sleep(3000 + random() * 1000);
        var flag = text("领取红包封面").exists(); 
        toast(flag);
        if (flag){
            id("b3z").findOne().children().forEach(child => {
                var target = child.findOne(id("fky"));
                target.click();
            })
            break;
        }else{
            id("fkw").findOne().click();
        }
    }
    toast("恭喜恭喜");
}

function alipay(){
    var name = getPackageName("支付宝");
    app.launchPackage(name);

    var ali_vip_page = "alipays://platformapi/startapp?appId=20000160";

    app.startActivity({
        action: "VIEW",
        data: ali_vip_page,
    });
}
// 事件监听
function volKeys() {
    let _keyMsg = (e) => {
        let _keyCodeStr = android.view.KeyEvent.keyCodeToString;
        let _code = e.getKeyCode();
        let _name = _keyCodeStr(_code);
        return _name + " (" + _code + ")";
    };

    threads.start(function () {
        events.observeKey();
        events.setKeyInterceptionEnabled("volume_up", true);
        events.onceKeyDown("volume_up", function (e) {
            toast(_keyMsg(e));
            engines.stopAllAndToast();
        });
        events.setKeyInterceptionEnabled("volume_down", true);
        events.onceKeyDown("volume_down", function (e) {
            toast(_keyMsg(e));
        });
    });

    return this;
}
